package trade

import (
	"bytes"
	"encoding/xml"
	"io/ioutil"
	"net/http"
	"strings"

	"go-papers/common/go-wechat/common"
)

func (this *CWechatPay) AddShare(param *common.OrderAddShare) (*common.OrderShareResult, error) {
	param.AppId = this.m_userInfo.AppId
	param.MchId = this.m_userInfo.MchId
	//param.SubMchId = this.m_userInfo.SubMchId
	param.NonceStr = randomNonceStr()

	var m map[string]interface{}
	m = make(map[string]interface{}, 0)
	m["appid"] = param.AppId
	m["mch_id"] = param.MchId
	m["sub_mch_id"] = param.SubMchId
	m["nonce_str"] = param.NonceStr
	m["receiver"] = param.Receivers
	param.Sign = GetSignV2(m, this.m_userInfo.AppSecret)

	bytes_req, err := xml.Marshal(param)
	if err != nil {
		return nil, err
	}

	str_req := string(bytes_req)
	str_req = strings.Replace(str_req, "OrderAddShare", "xml", -1)
	bytes_req = []byte(str_req)

	//发送unified order请求.
	req, err := http.NewRequest("POST", common.ADD_SHARE_URL, bytes.NewReader(bytes_req))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Accept", "application/xml")
	req.Header.Set("Content-Type", "application/xml;charset=utf-8")

	w_req := http.Client{
		Transport: WithCertBytes(this.m_userInfo.ApiclientCert, this.m_userInfo.ApiclientKey),
	}

	resp, _err := w_req.Do(req)
	if _err != nil {
		return nil, err
	}
	body, _ := ioutil.ReadAll(resp.Body)

	var refund_resp common.OrderShareResult

	_err = xml.Unmarshal(body, &refund_resp)
	if _err != nil {
		return nil, err
	}
	return &refund_resp, nil
}

func (this *CWechatPay) ShareReturn(param *common.OrderShareReturn) (*common.OrderShareReturnResult, error) {

	param.AppId = this.m_userInfo.AppId
	param.MchId = this.m_userInfo.MchId
	//param.SubMchId = this.m_userInfo.SubMchId
	param.NonceStr = randomNonceStr()

	var m map[string]interface{}
	m = make(map[string]interface{}, 0)
	m["appid"] = param.AppId
	m["mch_id"] = param.MchId
	m["sub_mch_id"] = param.SubMchId
	m["order_id"] = param.OrderId
	m["out_order_no"] = param.OutTradeNo
	m["out_return_no"] = param.OutReturnNo
	m["return_account_type"] = param.ReturnAccountType
	m["return_account"] = param.ReturnAccount
	m["return_amount"] = param.ReturnAmount
	m["description"] = param.Description
	m["nonce_str"] = param.NonceStr
	param.Sign = GetSignV2(m, this.m_userInfo.AppSecret)

	bytes_req, err := xml.Marshal(param)
	if err != nil {
		return nil, err
	}

	str_req := string(bytes_req)
	str_req = strings.Replace(str_req, "OrderShareFinish", "xml", -1)
	bytes_req = []byte(str_req)

	//发送unified order请求.
	req, err := http.NewRequest("POST", common.SHARE_FINISH_URL, bytes.NewReader(bytes_req))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Accept", "application/xml")
	req.Header.Set("Content-Type", "application/xml;charset=utf-8")

	w_req := http.Client{
		Transport: WithCertBytes(this.m_userInfo.ApiclientCert, this.m_userInfo.ApiclientKey),
	}

	resp, _err := w_req.Do(req)
	if _err != nil {
		return nil, err
	}
	body, _ := ioutil.ReadAll(resp.Body)

	var refund_resp common.OrderShareReturnResult

	_err = xml.Unmarshal(body, &refund_resp)
	if _err != nil {
		return nil, err
	}
	return &refund_resp, nil
}

// ShareFinish 完结分账分账
func (this *CWechatPay) ShareFinish(param *common.OrderShareFinish) (*common.OrderShareResult, error) {

	param.AppId = this.m_userInfo.AppId
	param.MchId = this.m_userInfo.MchId
	//param.SubMchId = this.m_userInfo.SubMchId
	param.NonceStr = randomNonceStr()

	var m map[string]interface{}
	m = make(map[string]interface{}, 0)
	m["appid"] = param.AppId
	m["mch_id"] = param.MchId
	m["sub_mch_id"] = param.SubMchId
	m["transaction_id"] = param.TransactionId
	m["out_order_no"] = param.OutTradeNo
	m["description"] = param.Description
	m["nonce_str"] = param.NonceStr
	param.Sign = GetSignV2(m, this.m_userInfo.AppSecret)

	bytes_req, err := xml.Marshal(param)
	if err != nil {
		return nil, err
	}

	str_req := string(bytes_req)
	str_req = strings.Replace(str_req, "OrderShareFinish", "xml", -1)
	bytes_req = []byte(str_req)

	//发送unified order请求.
	req, err := http.NewRequest("POST", common.SHARE_FINISH_URL, bytes.NewReader(bytes_req))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Accept", "application/xml")
	req.Header.Set("Content-Type", "application/xml;charset=utf-8")

	w_req := http.Client{
		Transport: WithCertBytes(this.m_userInfo.ApiclientCert, this.m_userInfo.ApiclientKey),
	}

	resp, _err := w_req.Do(req)
	if _err != nil {
		return nil, err
	}
	body, _ := ioutil.ReadAll(resp.Body)

	var refund_resp common.OrderShareResult

	_err = xml.Unmarshal(body, &refund_resp)
	if _err != nil {
		return nil, err
	}
	return &refund_resp, nil
}

func (this *CWechatPay) Share(param *common.OrderShare) (*common.OrderShareResult, error) {

	param.AppId = this.m_userInfo.AppId
	param.MchId = this.m_userInfo.MchId
	//param.SubMchId = this.m_userInfo.SubMchId
	param.NonceStr = randomNonceStr()

	var m map[string]interface{}
	m = make(map[string]interface{}, 0)
	m["appid"] = param.AppId
	m["mch_id"] = param.MchId
	m["sub_mch_id"] = param.SubMchId
	m["transaction_id"] = param.TransactionId
	m["out_order_no"] = param.OutTradeNo
	m["nonce_str"] = param.NonceStr
	m["receivers"] = param.Receivers
	param.Sign = GetSignV2(m, this.m_userInfo.AppSecret)

	bytes_req, err := xml.Marshal(param)
	if err != nil {
		return nil, err
	}

	str_req := string(bytes_req)
	str_req = strings.Replace(str_req, "OrderShare", "xml", -1)
	bytes_req = []byte(str_req)

	//发送unified order请求.
	req, err := http.NewRequest("POST", common.SHARE_URL, bytes.NewReader(bytes_req))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Accept", "application/xml")
	req.Header.Set("Content-Type", "application/xml;charset=utf-8")

	w_req := http.Client{
		Transport: WithCertBytes(this.m_userInfo.ApiclientCert, this.m_userInfo.ApiclientKey),
	}

	resp, _err := w_req.Do(req)
	if _err != nil {
		return nil, err
	}
	body, _ := ioutil.ReadAll(resp.Body)

	var refund_resp common.OrderShareResult

	_err = xml.Unmarshal(body, &refund_resp)
	if _err != nil {
		return nil, err
	}
	return &refund_resp, nil
}
